﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using MShop.Core.Domain;

namespace MShop.Web.Components.Extensions
{
    public static class HtmlExtensions 
    {
        public static  string Image(this HtmlHelper html,string imageUrl)
        {
                TagBuilder tag=new TagBuilder("img");
                tag.Attributes.Add("src", imageUrl);
                return tag.ToString();
        }
        public static string Image(this HtmlHelper html, string imageUrl,object htmlAttributes)
        {           
            
            TagBuilder tag=new TagBuilder("img");
            new System.Web.Routing.RouteValueDictionary(htmlAttributes).ToList().ForEach(
                (x) => {
                    tag.Attributes.Add(x.Key, x.Value.ToString());
                }
                );
            tag.MergeAttribute("src",imageUrl);
            return tag.ToString();
        }

        public static string ActionImageLink(this HtmlHelper html, string imageUrl,string actionName,string controllerName,object routeValues,object htmlAttributes)
        {            
            TagBuilder tag = new TagBuilder("a");
            tag.InnerHtml=(string.Format("<img src='{0}' />", imageUrl));
            if (string.IsNullOrEmpty(controllerName))
            {
                controllerName = html.ViewContext.RouteData.Values["controller"].ToString();
            }
          
         
            string url=  UrlHelper.GenerateUrl(null,actionName,controllerName,new RouteValueDictionary(routeValues),html.RouteCollection,html.ViewContext.RequestContext,
                true);
            var dicthtmlAttribute = new System.Web.Routing.RouteValueDictionary(htmlAttributes);
            tag.MergeAttribute("href", url);
            tag.MergeAttributes<string, object>(dicthtmlAttribute);
          
            return tag.ToString();
        }

        public static string ActionImageLink(this HtmlHelper html, string imageUrl, string actionName,  object routeValues, object htmlAttributes)
        {
            return html.ActionImageLink(imageUrl, actionName, null, routeValues, htmlAttributes);
        }

        public static string ActionImageLink(this HtmlHelper html, string imageUrl, string actionName, object routeValues)
        {
            return html.ActionImageLink(imageUrl, actionName, null, routeValues, null);
        }


        public static IList<SelectListItem> ExamineRightFlags(this HtmlHelper html,IList<SelectListItem> listitem, RightFlags flags)
        {
            
            foreach (var sitem in listitem)
            {
                RightFlags value = (RightFlags)int.Parse(sitem.Value);
                if ((flags & value) == value)
                {
                    sitem.Selected = true;
                }
                else
                {
                    sitem.Selected = false;
                }
            }
            return listitem;
        }
    }
}
